package com.tj.collection.task;

import com.dangdang.ddframe.job.api.ShardingContext;
import com.dangdang.ddframe.job.api.simple.SimpleJob;
import com.fqgj.common.api.Page;
import com.fqgj.common.utils.CollectionUtils;
import com.fqgj.log.factory.LogFactory;
import com.fqgj.log.interfaces.Log;
import com.google.common.collect.Lists;
import com.tj.collection.bean.SystemConstants;
import com.tj.collection.service.ClOutboundService;
import com.tj.collection.utils.ConfigUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;

/**
 * Created with IntelliJ IDEA.
 * User: pavan
 * Date: 2020/4/24
 * Time: 下午1:06
 */
@Component
public class OutboundTaskJob implements SimpleJob {

    private static final Log LOGGER = LogFactory.getLog( OutboundTaskJob.class);

    @Autowired
    private ConfigUtil configUtil;

    @Autowired
    private ClOutboundService clOutboundService;

    @Override
    public void execute(ShardingContext shardingContext) {
        if (!configUtil.getOutboundTaskSwitch()) {
            LOGGER.info("D0呼叫,开关未开启");
            return;
        }

        List<String> list = Lists.newArrayList(SystemConstants.MERALOAN, SystemConstants.DHANADHANV6);
        LOGGER.info("D0呼叫开始");
        int i = 1;
        while (true) {
            if (CollectionUtils.isEmpty(list)) {
                break;
            }
            LOGGER.info("D0呼叫处理中,第{}次", i);
            List<String> toRemove = new ArrayList<>();
            for (String merchantCode : list) {
                int num1 = 0;
                if (SystemConstants.MERALOAN.equals(merchantCode)) {
                    LOGGER.info("D0呼叫处理中,第{}次,MERALOAN openapi 来的用户不呼叫", i);
                } else {
                    num1 = clOutboundService.addCallTask(merchantCode, 0, "open_api", new Page(100));
                }
                int num2 = clOutboundService.addCallTask(merchantCode, 0, "not_open_api", new Page(100));
                if (num1 == 0 && num2 == 0) {
                    toRemove.add(merchantCode);
                }
            }
            if (CollectionUtils.isNotEmpty(toRemove)) {
                for (String s : toRemove) {
                    list.remove(s);
                    LOGGER.info("D0呼叫,商户移除,{}", s);
                }
            }
            i++;
        }
        LOGGER.info("D0呼叫结束");
    }
}
